from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from user import models as um, userser
from home import models as hm, homeser


# Create your views here.
class HrInfoView(APIView):
    def get(self, request):
        user = request.query_params.get('user')
        hr = um.HRInfo.objects.filter(user=user).first()
        if not hr:
            return Response({"code": 400, "msg": "用户不存在"})

        hr_ser = userser.HRInfoSerializer(hr)
        company = um.Company.objects.filter(id=hr.company.id)
        company_ser = userser.CompanySerializer(company, many=True)
        return Response({"code": 200, "mgs": "查询成功", "data": {"hr": hr_ser.data, "company": company_ser.data}})


class JobPostingView(APIView):
    def get(self, request):
        hr = request.query_params.get('hr')
        jobposting = hm.Jobposting.objects.filter(hr=hr)
        jobposting_ser = homeser.JobpostingSer(jobposting, many=True)
        return Response({"code": 200, "msg": "查询成功", "data": jobposting_ser.data})

    def post(self, request):
        ser = homeser.JobpostingSerializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200, "msg": "发布成功", "data": ser.data})
        else:
            return Response({"code": 400, "msg": "发布失败", "data": ser.errors})
